<?php namespace App\Http\Controllers\Site;

use App\Http\Controllers\Controller;
use GuzzleHttp\Psr7\Response;
use Illuminate\Http\Request;
use DB;
use App\Item;

class IndexController extends Controller
{
    public function getIndex(Request $request)
    {
        $work = Item::orderBy("updated_at","desc")->where('round','>',0)->orderBy("id", 'desc')->paginate(3);
        return view('site.index',[
            'work'=>$work,
        ]);
    }

    public  function getUploadwork()
    {
        $key = 20;
        $type = Config("site.items.index.type");
        $value = "nin";
        return view('site.uploadwork', [
            'key' => $key,
            'type' => $type,
            'value' => $value,
        ]);
    }

    public function getAppuploadwork(){
        $key = 20;
        $type = Config("site.items.index.type");
        $value = "nin";
        return view('site.appuploadwork', [
            'key' => $key,
            'type' => $type,
            'value' => $value,
        ]);
    }

    public  function getAppindex(Request $request){
        $data = $request->all();

        if(isset($data['month']) && isset($data['round'])){
//            echo $data['month']; exit();
            $work = Item::orderBy("work.updated_at","desc")
                ->leftJoin('pictures', 'work.pic', '=', 'pictures.id')
                ->select('work.*', 'pictures.dir', 'pictures.sub_dir', 'pictures.pic as picname')
                ->where('work.add_month', $data['month'])
                ->where('work.round', '>=', $data['round'])
                ->paginate(4);
            return response()->json($work);
        }else{
            $month = isset($data['month']) ? $data['month'] : '2016-07';
            $round = isset($data['round']) ? $data['round'] : '1';
            $work = Item::orderBy("work.updated_at","desc")
                ->leftJoin('pictures', 'work.pic', '=', 'pictures.id')
                ->select('work.*', 'pictures.dir', 'pictures.sub_dir', 'pictures.pic as picname')
                ->where('work.add_month', $month)
                ->where('work.round', '>=', $round)
                ->paginate(4);
            return view('site.appindex', [
                'work'=>$work,
            ]);
        }
    }

    public function getAppdetail(){
        return view('site.appdetail');
    }

    //投票
    public function postVote(Request $request){
        $data = $request->all();
        $count = DB::table("vote")->where("workdid",$data['id'])->where("ip", $data['ip'])->whereBetween('created_at',[date('Y-m-d 00:00:00',time()) , date('Y-m-d 23:59:59',time())])->count();
        if($count>0){
            $ret = ['state' => 'fail', 'message' => '今天您已经给这个作品票！'];
        }else{
            DB::transaction(function () use($data){
                DB::table("work")->where("id", $data["id"])->increment("vote");
                DB::table("vote")->insert(["workdid"=>$data["id"], "ip"=>$data['ip']]);
            });
            $ret = ['state' => 'success', 'message' => '投票成功'];
        }
        return response()->json($ret);
    }

    public  function uploadwork(Request $request){

        $data = $request->all();

        if(Item::where('name', $request['name'])->count() > 0){
            $ret = ['state' => 'fail', 'message' => '该照片已经存在'];
            return \Response::json($ret);
        }

        $object = Item::create([
            'name' => $request['name'],
            'pic_address' => $request['pic_address'],
            'add_time' => $request['add_time'],
            'add_month' => date('Y-m'),
            'pic' => $request['pic'],
            'author' => $request['author'],
            'authoraddress' => $request['authoraddress'],
            'mobile' => $request['mobile'],
            'agent'=>$_SERVER['HTTP_USER_AGENT'],
        ]);
        if($object && $object->id>0){
            $ret = ['state' => 'success', 'message' => '操作成功'];
        }else{
            $ret = ['state' => 'fail', 'message' => '操作失败'];
        }
        return \Response::json($ret);
    }


}